package com.yehaikeji.workflow.service.serviceImpl;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import com.yehaikeji.workflow.bean.Model;
import com.yehaikeji.workflow.mapper.ModelMapper;
import com.yehaikeji.workflow.service.ModelService;

@Service
public class ModelServiceImpl implements ModelService {

	@Autowired
	private ModelMapper modelMapper;
	
	@Override
	public int addModel(Model model) {
		if(StringUtils.isEmpty(model.getUserId()) || StringUtils.isEmpty(model.getName()) || StringUtils.isEmpty(model.getContent())){
			throw new RuntimeException("非空参数不能为空");
		}
		model.setId(UUID.randomUUID().toString().replaceAll("-", ""));
		model.setCreatedate(new Date());
		return modelMapper.insertSelective(model);
	}

	@Override
	public int deleteModel(String id) {
		return modelMapper.deleteByPrimaryKey(id);
	}

	@Override
	public List<Model> listPageModels(String name) {
		Map<String,String> paramMap = new HashMap<>();
		paramMap.put("name", name);
		return modelMapper.selectPageList(paramMap);
	}

	@Override
	public List<Model> queryAllModel() {
		return modelMapper.selectAll();
	}

	@Override
	public int modifyModel(Model model) {
		if(StringUtils.isEmpty(model.getId())){
			throw new RuntimeException("id不能为空");
		}
		Model returnModel = modelMapper.selectByPrimaryKey(model.getId());
		if(null == returnModel){
			throw new RuntimeException("id不正确");
		}
		returnModel.setContent(model.getContent());
		returnModel.setName(model.getName());
		return modelMapper.updateByPrimaryKeySelective(returnModel);
	}

}
